<template>
  <div>
    <el-row>
      <el-col :span="24">
        <el-breadcrumb  separator="/">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>活动管理</el-breadcrumb-item>
          <el-breadcrumb-item>活动列表</el-breadcrumb-item>
          <el-breadcrumb-item>活动详情</el-breadcrumb-item>
        </el-breadcrumb>
      </el-col>
      <el-col :span="24">
        <el-input v-model="query" placeholder="请输入内容" class="search-input" @keyup.native.enter="getList">
          <el-button slot="append" icon="el-icon-search" @click="getList"></el-button>
        </el-input>
        <el-button type="primary" @click="dialogFormVisible=true">添加用户</el-button>
      </el-col>
      <el-col :span="24">
        <el-table
          v-loading="loading"
          :data="users"
          border
          style="width: 100%">
          <el-table-column
            prop="username"
            label="姓名"
            width="180">
          </el-table-column>
          <el-table-column
            prop="email"
            label="邮箱"
            width="180">
          </el-table-column>
          <el-table-column
            prop="mobile"
            label="电话">
          </el-table-column>
          <el-table-column label="用户状态">
            <template slot-scope="scope">
              <el-switch v-model="scope.row.mg_state" @change="changeUserState(scope.row)"></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                plain
                size="mini"
                icon="el-icon-edit"
                type="primary"
                @click="handleEdit(scope.$index, scope.row)"></el-button>
              <el-button
                plain
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="handleDelete(scope.$index, scope.row)"></el-button>
              <el-button
                plain
                size="mini"
                type="warning"
                icon="el-icon-check"
                @click="showGrantDialog(scope.$index, scope.row)"></el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
      <el-col :span="24">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[2, 4, 6, 8]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </el-col>
    </el-row>
    <!-- 添加用户对话框 -->
    <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="addUserForm">
        <el-form-item prop="username" label="用户名" :label-width="formLabelWidth">
          <el-input  v-model="form.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item prop="password" label="密码" :label-width="formLabelWidth">
          <el-input  v-model="form.password" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item prop="email" label="邮箱" :label-width="formLabelWidth">
          <el-input  v-model="form.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item prop="mobile" label="电话" :label-width="formLabelWidth">
          <el-input  v-model="form.mobile" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUserSubmit">确 定</el-button>
      </div>
    </el-dialog>
    <!-- /添加用户对话框 -->
    <!-- 分配角色对话框 -->
    <el-dialog title="分配角色" :visible.sync="grantDialogVisible">
      <el-form :model="roleForm"  ref="roleUserForm">
        <el-form-item label="当前的用户">
          <el-tag type="info">{{roleForm.username}}</el-tag>
        </el-form-item>
        <el-form-item label="请选择角色">
          <el-select v-model="roleId"  placeholder="请选择角色">
            <el-option :label="role.roleName" :value="role.id" v-for="(role,index) in roleList" :key="role.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="grantDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="changeRole">确 定</el-button>
      </div>
    </el-dialog>
    <!-- /分配角色对话框 -->
  </div>
</template>
<script>
export default {
  data () {
    return {
      query:'',//搜索的关键词，默认是空
      tableData: [{
          date: '2016-05-02',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1518 弄'
        }, {
          date: '2016-05-04',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1517 弄'
        }, {
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄'
        }, {
          date: '2016-05-03',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1516 弄'
        }],
        currentPage:1,
        //存用户列表数据
        users:[],
        total:0,
        pagesize:2,
        dialogFormVisible: false,
        form: {
          username:'',
          password:'',
          mobile:''
        },
        formLabelWidth: '120px',
        rules:{
          username:[
            {required:true,message:'请输入用户名',trigger:'blur'}
          ],
          password:[
            {required:true,message:'请输入密码',trigger:'blur'}
          ],
          email:[
            {required:true,message:'请输入邮箱',trigger:'blur'},
            {type:'email',message:'请输入正确的邮箱地址',trigger:'blur,change'}
          ],
          mobile:[
            {required:true,message:'电话不能为空'}
          ]
        },
        roleForm:{},
        grantDialogVisible:false,
        roleList:[],//保存获取到的所有的角色
        roleId:'',
        loading:true 
    };
  },
  created() {
    this.getList( )
  },
  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pagesize = val
      this.getList() 
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val 
      this.getList()
    },
    getList(){
      this.loading = true 
      this.$http.get('users',{params:{query:this.query,pagenum:this.currentPage,pagesize:this.pagesize}}).then(res=>{
        // console.log(res);
        this.users = res.data.data.users 
        this.total = res.data.data.total
        this.loading = false 
      })
    },
    changeUserState(row){
      this.$http.put(`users/${row.id}/state/${row.mg_state}`).then(res=>{
        console.log(res);
        if(res.data.meta.status === 200){
          this.$message({
            type:'success',
            message:'修改用户状态成功'
          })
        }else{
          this.$message({
            type:'warning',
            message:res.meta.msg 
          })
        }
      })
    },
    addUserSubmit(){
      this.$refs['addUserForm'].validate(valid=>{
        if(valid){
          this.$http.post('users',this.form).then(res=>{
            console.log(res);
            if(res.data.meta.status == 201){
              this.dialogFormVisible = false
              this.total = res.data.data.total
              this.getList() 
            }
          })
        }else{
          console.log('验证失败');
          return false 
        }
      })
    },
    showGrantDialog(index,row){
      this.roleForm = row 
      this.grantDialogVisible = true 
      this.$http.get('roles').then(res=>{
        console.log(res);
        if(res.data.meta.status == 200){
          this.roleList = res.data.data
        }
      })
    },
    changeRole(){
      if(!this.roleId){
        this.$message({
          type:'warning',
          message:'角色不能为空'
        })
        return 
      }
      // console.log(this.roleId);
      this.$http.put(`users/${this.roleForm.id}/role`,{id:this.roleForm.id,rid:this.roleId}).then(res=>{
        // console.log(res);
        if(res.data.meta.status == 200){
          this.$message({
            type:'success',
            message:'设置角色成功'
          })
          this.grantDialogVisible = false 
        }else{
          this.$message({
            type:'error',
            message:res.data.meta.msg 
          })
        }
      })
    }
  },
}
</script>
<style lang="scss" scoped>
.el-breadcrumb{
  height:40px;
  line-height:45px;
  background:#ccc;
}
.search-input{
  width:30%;
}
</style>